//
//  HomeCellOriginalView.swift
//  SinaWeiBo
//
//  Created by 有风 on 16/2/21.
//  Copyright © 2016年 YouFeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HomeCellOriginalView: UIView {

    
    //添加约束对象属性 做记录使用
    var bottomConstaint: Constraint?
    
    var status: Status? {
        didSet{
            iconImageView.sd_setImageWithURL(status?.user?.profile_url)
            nameLabel.text = status?.user?.name
            //sourceLabel.text = status?.source
            rank_img.image = status?.user?.mbrank_image
            //verified_type_image.image = status?.user?.verified_type_image
            textLabel.text = status?.text
            pictureView.imageURLs = status?.imageURLs
            
            //解决约束冲突
            //1.先卸载老的约束对象  -> 使用属性 记录底部的约束对象
            self.bottomConstaint?.uninstall()
            //2.根据条件重新添加约束对象
            if let imageURLs = status?.imageURLs where imageURLs.count != 0 {
                //有配图视图
                //原创微博底部相对于配图视图进行约束  同一条约束存在就执行更新 不存在就执行添加约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                //没有配图视图
                //原创微博底部相对于微博正文进行约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    // constraint 获取自动布局的约束对象
                    self.bottomConstaint = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }

            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.brownColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(rank_img)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verified_type_image)
        addSubview(textLabel)
        addSubview(pictureView)
        textLabel.backgroundColor = UIColor.yellowColor()
        
        //
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            //约束大小
            make.size.equalTo(CGSize(width: 40, height: 40))

        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMargin)
            make.top.equalTo(iconImageView.snp_top)

        }
        
        rank_img.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(timeLabel.snp_top)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            //在右下角位置
            make.right.equalTo(iconImageView.snp_right)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }

        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(textLabel.snp_left)
            //右边的约束不需要添加  在四张图片显示的时候 配图视图的布局有变化
            //            make.right.equalTo(textLabel.snp_right)
            //设置测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }


        self.snp_makeConstraints { (make) -> Void in
            
            //make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            
        }
        
        //将用户头像处理成圆角
        iconImageView.layer.cornerRadius = 20
        iconImageView.layer.masksToBounds = true


        

        
    }
    
    
    
    
    
    
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLabel: UILabel = UILabel(text: "test_name", fontSize: 15, textColor: themeColor)
    
    private lazy var timeLabel: UILabel = UILabel(text: "11:11", fontSize: 12, textColor: UIColor.lightGrayColor())
    
    private lazy var sourceLabel: UILabel = UILabel(text: "test_source", fontSize: 12, textColor: UIColor.lightGrayColor())
    
    private lazy var rank_img: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var verified_type_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var textLabel: UILabel = UILabel(text: "test_text", fontSize: 14, textColor: UIColor.blackColor(),alignment: NSTextAlignment.Left)
    
    //配图视图
    private lazy var pictureView: HomeCellPictureView = HomeCellPictureView()
    
}
